package org.komarichin.jpa.entity;

import javax.persistence.*;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 20.09.2009
 * Time: 17:18:35
 */
@Entity(name = "Role")
@Table(name = "ROLE")
@NamedQueries({
        @NamedQuery(name = "role.findAll", query = "select r from Role r"),
        @NamedQuery(name = "role.findByName", query = "select r from Role r where r.name = :name"),
        @NamedQuery(name = "role.findAllowRolesByPermission", query = "select r from Role r where r.permission >= :permission")
})
public class Role implements IRole {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Long permission;


    /**
     * {@inheritDoc}
     */
    public Long getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    public void setId(Long id) {
        this.id = id;
    }

//    public Key getId() {
//        return id;
//    }
//
//    public void setId(Key id) {
//        this.id = id;
//    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return name;
    }

    /**
     * {@inheritDoc}
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    public Long getPermission() {
        return permission;
    }

    /**
     * {@inheritDoc}
     */
    public void setPermission(Long permission) {
        this.permission = permission;
    }


    //    /**
//     * {@inheritDoc}
//     */
//    @Override
//    public Key getKey() {
//        return id;
//    }
//
//    /**
//     * {@inheritDoc}
//     */
//    @Override
//    public void setKey(Key key) {
//        this.id = key;
//    }

    @Override
    @Transient
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Role");
        sb.append("{id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", permission=").append(permission);
        sb.append('}');
        return sb.toString();
    }

    @Override
    @Transient
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Role)) return false;

        Role role = (Role) o;

        if (id != null ? !id.equals(role.id) : role.id != null) return false;
        if (name != null ? !name.equals(role.name) : role.name != null) return false;
        if (permission != null ? !permission.equals(role.permission) : role.permission != null) return false;

        return true;
    }

    @Override
    @Transient
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (permission != null ? permission.hashCode() : 0);
        return result;
    }
}
